package com.yanqu.road.server.gameplayer.module.activity.tomb.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class TombUserChangeScoreServiceCmd implements IGameServiceCommand {

    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        Tomb.TombUserScoreChangeCrossToGameReq req = Tomb.TombUserScoreChangeCrossToGameReq.parseFrom(request.getDataBytes());
        long userId = req.getUserId();
        long addScore = req.getUnionScore();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(req.getUserId());
        if (null != player) {
            //商战积分冲榜
            player.notifyListener(eGamePlayerEventType.TombUnionRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(req.getUnionScore()), player.getUserInfo().getUnionUid()));
        } else {
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo == null) {
                return syncResponse;
            }
            ActivityInfo activityInfo = TombMgr.getActivityInfo();
            if (activityInfo == null) {
                return syncResponse;
            }
            List<ActivityConditionInfo> activityConditionInfoList = NormalActivityMgr.getActivityConditionInfoListByType(activityInfo.getActivityId(), eGamePlayerEventType.TombUnionRank.getValue());
            UserActivityConditionData userActivityConditionData = null;
            if (null != activityConditionInfoList && activityConditionInfoList.size() > 0) {
                userActivityConditionData = UserActivityBussiness.getUserActivityConditionData(activityInfo.getActivityId(), userId, activityConditionInfoList.get(0).getConditionId());
                if (null == userActivityConditionData) {
                    userActivityConditionData = NormalActivityMgr.initUserActivityConditionData(userId, UserMgr.getNickName(userId), BigInteger.valueOf(addScore),
                            activityInfo.getActivityId(), activityConditionInfoList.get(0).getConditionId(), activityConditionInfoList.get(0).getType());
                    UserActivityBussiness.addUserActivityConditionData(userActivityConditionData);
                } else {
                    userActivityConditionData.setValue(BigInteger.valueOf(addScore));
                    UserActivityBussiness.updateUserActivityConditionData(userActivityConditionData);
                }
            }
            if (null != userActivityConditionData) {
                String unionUid = userInfo.getUnionUid();
                if (!StringUtils.isNullOrEmpty(unionUid)) {
                    UnionActivityMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, userId, userActivityConditionData, UnionRiseRankArgs.NOTIFY_TYPE_NORMAL);
                }
            }
        }

        return syncResponse;
    }
}
